<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <!--搜索-->
          <SearchUiNew ref="SearchUiNew" style="margin-top: 10px" :searchFormArr="searchFormArr" @searchParma="searchParma"></SearchUiNew>
        </div>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <div class="panel-line flex-end" style="margin-bottom: 20px">
          <el-button type="primary" @click="handleAdd" size="mini">+ 新增</el-button>
        </div>
        <el-table stripe :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" @selection-change="handleSelectionChange" v-loading="dataListLoading">
          <el-table-column type="selection" fixed="left" width="55" :selectable="selectable"> </el-table-column>
          <el-table-column align="center" header-align="center" label="账号" prop="account"></el-table-column>
          <el-table-column align="center" header-align="center" label="姓名" prop="name"></el-table-column>
          <el-table-column align="center" header-align="center" label="部门" prop="dept_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="角色" prop="role_name"></el-table-column>
          <el-table-column align="center" header-align="center" label="状态" prop="status">
            <template slot-scope="scope">
              {{ scope.row.status === 1 ? '启用' : '停用' }}
            </template>
          </el-table-column>
          <el-table-column align="center" header-align="center" label="操作">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleEdit(scope)" :disabled="scope.row.account === 'admin'">编辑</el-button>
              <el-button size="mini" type="text" :class="{ 'button-danger': scope.row.account !== 'admin' }" @click="handleDelete([scope.row.id])" :disabled="scope.row.account === 'admin'">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
        <div class="panel-line" style="margin-top: 20px">
          <el-button type="primary" @click="handleOn" size="mini" :disabled="multipleSelection.length === 0">批量启用</el-button>
          <el-button type="danger" @click="handleOff" size="mini" :disabled="multipleSelection.length === 0">批量停用</el-button>
          <el-button type="danger" @click="handleDeleteMultiple" size="mini" :disabled="multipleSelection.length === 0">批量删除</el-button>
        </div>
      </div>
    </div>

    <!--  添加 -->
    <el-drawer :title="editModel === 'add' ? '编辑员工' : '新增员工'" :visible.sync="showDialogAdd" size="50%">
      <DialogAdd v-if="showDialogAdd" :editModel="editModel" :info="currentItem" :roleList="roleList" :deptList="deptList" @close="showDialogAdd = false" @success="handleAddSuccess" @resetPassword="showDialogResetPassword = true" />
    </el-drawer>

    <!--  修改密码 -->
    <el-dialog title="修改密码" :visible.sync="showDialogResetPassword" width="40%">
      <DialogResetPassword v-if="showDialogResetPassword" :id="currentItem.id" @close="showDialogResetPassword = false" @success="showDialogResetPassword = false" />
    </el-dialog>
  </div>
</template>

<script>
import { apiUsers, apiUserDel } from '@/api/assetsPublic'
import { apiSysUserSts, apiSysRolesList, apiSysDeptSelect } from '@/api/system'
import DialogAdd from './components/DialogAdd.vue'
import DialogResetPassword from './components/DialogResetPassword.vue'

export default {
  name: 'Account',
  components: { DialogAdd, DialogResetPassword },
  data() {
    return {
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
      currentItem: {},
      showDialogAdd: false,
      showDialogResetPassword: false,
      editModel: 'add',
      searchFormArr: [
        {
          fieldName: '账号:',
          type: 'input',
          field: 'account',
          placeholder: '请输入账号',
        },
        {
          fieldName: '姓名:',
          type: 'input',
          field: 'name',
          placeholder: '请输入姓名',
        },
        {
          fieldName: '部门:',
          type: 'cascader',
          field: 'dept_id',
          props: { value: 'id', label: 'name', checkStrictly: true },
          option: [],
        },
        {
          fieldName: '角色:',
          type: 'select',
          field: 'role_id',
          option: [],
        },
        {
          fieldName: '状态:',
          type: 'select',
          field: 'status',
          option: [
            { value: '1', label: '启用' },
            { value: '0', label: '停用' },
          ],
        },
      ],
      searchParams: {
        account: '',
        name: '',
        dept_id: '',
        role_id: '',
        status: '',
      },
      multipleSelection: [],
      deptList: [],
      roleList: [],
    }
  },
  activated() {
    this.$refs.table.doLayout()
  },
  mounted() {
    this.init()
  },
  destroyed() {},
  methods: {
    init() {
      this.getSelect()
      this.query = this.$route.query
      const { role_id } = this.query
      if (role_id) {
        this.searchParams.role_id = role_id
      }

      this.$nextTick(() => {
        const setItemList = []
        if (role_id) {
          setItemList.push({ key: 'role_id', value: role_id })
        }
        if (setItemList.length > 0) {
          this.$refs.SearchUiNew.setItemValue(setItemList)
        } else {
          this.getDataList()
        }
      })
    },
    getSelect() {
      apiSysDeptSelect().then((res) => {
        this.deptList = res.data
        const item = this.searchFormArr.find((item) => item.field === 'dept_id')
        item.option = this.deptList
      })
      apiSysRolesList().then((res) => {
        this.roleList = res.data
        const item = this.searchFormArr.find((item) => item.field === 'role_id')
        item.option = this.roleList.map((item) => {
          return {
            value: item.id,
            label: item.name,
          }
        })
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let info = { pageNumber: this.pageNumber, pageSize: this.pageSize }
      info = Object.assign(info, this.searchParams)
      apiUsers(info)
        .then((res) => {
          const { data, total } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 列表查询
    searchParma(e) {
      this.searchFormArr.forEach((item) => {
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.dept_id && e.dept_id.length > 0) {
        this.searchParams.dept_id = e.dept_id[e.dept_id.length - 1]
      } else {
        this.searchParams.dept_id = ''
      }
      this.pageNumber = 1
      this.getDataList()
    },
    // 添加
    handleAdd() {
      this.editModel = 'add'
      this.showDialogAdd = true
    },
    // 编辑
    handleEdit(scope) {
      this.editModel = 'edit'
      this.currentItem = scope.row
      this.showDialogAdd = true
    },
    handleDeleteMultiple() {
      this.handleDelete(this.multipleSelection.map((item) => item.id))
    },
    // 删除
    handleDelete(ids) {
      window.enterPreventDefault = false
      this.$confirm('确定要删除这个账号？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          window.enterPreventDefault = true
          this.dataListLoading = true
          apiUserDel({ ids })
            .then((res) => {
              this.dataListLoading = false
              this.$message({
                type: 'success',
                message: '删除成功',
              })
              this.getDataList()
            })
            .catch(() => {
              this.dataListLoading = false
            })
        })
        .catch(() => {
          window.enterPreventDefault = true
        })
    },
    // 添加成功
    handleAddSuccess() {
      this.showDialogAdd = false
      this.getDataList()
    },
    // 表格多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleOn() {
      apiSysUserSts({
        status: 1,
        ids: this.multipleSelection.map((item) => item.id),
      }).then((res) => {
        this.$message({
          type: 'success',
          message: '启用成功',
        })
        this.getDataList()
      })
    },
    handleOff() {
      apiSysUserSts({
        status: 0,
        ids: this.multipleSelection.map((item) => item.id),
      }).then((res) => {
        this.$message({
          type: 'success',
          message: '停用成功',
        })
        this.getDataList()
      })
    },
    selectable(row, index) {
      if (row.account === 'admin') {
        return false
      } else {
        return true
      }
    },
  },
}
</script>

<style>
.el-cascader-menu__wrap {
  height: 200px !important;
}
</style>
